<?php

use Engine\Action\Helper;

class RegisterController extends Zend_Controller_Action {
    public function  init() {
//        $this->_helper->layout->setLayout('auto_new');
        
//         $phones_manager = new \Ria\Application\Manager\Phones();
//         $phoneObj = $phones_manager->deletePhonesByUser(369484);
//        print_r($phoneObj);
//        die;
//        $phoneArr = $phones_manager->getPhonesByUserOutputArray(369484);
//        print_r($phoneArr);
//        die;
//        $phones_manager->getPhonesByUser(369484);die;
//        $this->view->headScript()->appendFile('/js/registrationFunctions.js','text/javascript');
//        print_r(Zend_Auth::getInstance()->getIdentity());die;
    }

    public function indexAction() {
        
        
        $registrationHelper = new Helper\Registration();
        $loginHelper = new Helper\Login();
//        
//      $login_helper = new \App\Action\Helper\Login();
//        
        $registrationForm = $loginHelper->getForm();
        
        if (Zend_Auth::getInstance()->hasIdentity()){
           $this->_redirect(array(), './two');  
        }else{
            
        }
//        $registrationForm->generateDynamicElements($this->getRequest()->getPost());
        $registrationForm->populate($this->getRequest()->getPost());
        
        
        
        print_r($this->getRequest()->getPost());
//        
//        
//        
//        $ajax=false;
//        if ($this->getRequest()->isXmlHttpRequest()) {
//            $profile_url = '?target=user';
//            $exit_url = '?exit=yes';
//            $ajax=true;
//            $this->_helper->AjaxContext()->addActionContext('index', 'json')->initContext('json');
//            
////            print_r($this->getRequest()->getPost());die;
//        }else{
//            if (Zend_Auth::getInstance()->hasIdentity()){
//                $this->_redirect ('?target=user');
//            }else{
//                
//                Navigator::getInstance()->addPage('Автомобили', $this->view->url(array(), 'base'));
//                Navigator::getInstance()->addPage('Регистрация', $this->view->url(array(), 'autoinfo'));
//    //            $this->view->headScript()->appendFile('/js/ajaxArea.js','text/javascript');
//
//                $locationManger = new Managers\Passport\Location();
//
//                $states = $locationManger->getStates();
//                // making associated array from $states
//                if (count($states)) {
//                    $registrationForm->getElement('state')
//                                ->addMultiOption(0,_('Select'))
//                                ->addMultiOptions($states);
//                }
//
//                
//                
//                if (!$this->getRequest()->getParam('state')){
//                    $registrationForm->getElement('city')->addMultioption(0, _('Select'));
//                }else{
//                    $registrationForm->getElement('city')->addMultioption(0, _('Select city'));
//                    $cities = $locationManger->getCities($this->getRequest()->getParam('state')*1);
//                    $registrationForm->getElement('city')->addMultiOptions($cities);
//                }
//            }
//            
//        }
//        
//        
//        $registrationForm->generateDynamicElements($this->getRequest()->getPost());
//        $registrationForm->populate($this->getRequest()->getPost());
//            
//         
//        if ($this->getRequest()->isPost()) {
//                $userManager = new \Managers\Passport\Users();
//                
//                $tempUser = $userManager->getUserByEmail($this->getRequest()->getParam('email'));
////                $tempUser = $userManager->checkEmail($this->getRequest()->getParam('email'));
////                $tempUser = $userManager->checkEmail('698423@i.ua');
//
//                
//                
//            
//            if ($registrationForm->isValid($this->getRequest()->getPost())) {
                
//                $session = new Zend_Session_Namespace('temporary');
//                $session->unsetAll('temporary');
//                $userIp = ip2long($this->getRequest()->getServer('REMOTE_ADDR'));
//                $newUser = $registrationHelper->registrate($this->getRequest()->getParams(), $userIp);
                
                
                
                
//                $login_helper->authenticate($new_user->getEmail(), $new_user->getPassword());
                
                
//                
//                
//                
//                if ($new_user){
//                    $login_helper->authenticate($new_user->getEmail(), $new_user->getPassword());
//                    $phonesManager = new \Ria\Application\Manager\Phones();
//                    $phones = $phonesManager->getPhonesByUser($new_user->getClientId())->getResult();
//                    $phoneStr = '';
//                    foreach ($phones as $phone) {
//                        $phoneStr.=$phone->getPhoneStr() . ';   ';
//                    }
//                    $user_phones = $phoneStr;
//                    $user = $new_user;
//                    if (method_exists($user, 'getClientsNick') && $user->getClientsNick()) {
//                        $nick = $user->getClientsNick()->getPhoto();
//                        if ($nick) {
//                            $user_photo = $this->view->getImage($nick, 's');
//                        } else {
//                            $user_photo = 'http://auto.ria.ua/img/common/test/photo-smoll.png';
//                        }
//                    } else {
//                        $user_photo = 'http://auto.ria.ua/img/common/test/photo-smoll.png';
//                    }
//                    if($ajax){
//                        $this->view->status = 'ok';
//                        $name = array();
//                        $name['name'] = $new_user->getFirstName() . ' ' .$new_user->getLastName();
//                        $name['url'] = $profile_url;
//                        $name['title'] = _('Change profile');
//                        $name['exit_url'] = $exit_url;
//                        $name['exit'] = _('Exit');
//                        $name['phones'] = $user_phones;
//                        $name['photo'] = $user_photo;
//                        $this->view->name=$name;
//                    } else {
//                        $this->_redirect('?target=user');
//                    }
//                }
//            }else{
//                $errArr = $registrationForm->getMessages();
//                if (isset($errArr)){
//                    foreach($errArr as $key=>&$value){
//                        $str = '';
//                        if (is_array($value)){
//                            $ready = true;
//                            foreach($value as $k=>$it){
//                                if (is_array($it)){
//                                    foreach($it as $phErrKey => $phErrVal){
//                                        if (is_array($phErrVal)){
//                                            foreach($phErrVal as $ke=>$i)
//                                                $str .= $i.' ';
//                                        }else $str .= $phErrVal.' ';
//                                        $value[$k] = $str;
//                                        $ready = false;
//                                    }
//                                    $str = '';
//                                }else $str .= $it.' ';
//                            }
//                            if ($ready) $value = $str;
//                        }
//                    }
//                    $this->view->errors = $errArr;
//                }
//            
//            }
//            
//        }
//        
//        
        $this->view->form = $registrationForm;
//        
//
//
////        $registrationForm->setAction('/user/register/index');
//       
        
    }

    public function loginAction(){
        

        
        $loginHelper = new Helper\Login();
        $loginForm = $loginHelper->getForm(array('submit' => true));
        
        $this->view->form = $loginForm;
        
        if ($loginForm->isValid($this->getRequest()->getPost())) { 
            $email = $loginForm->getValue('email');
            $password = $loginForm->getValue('password');
            $session = new Zend_Session_Namespace('temporary');
            $session->unsetAll('temporary');
            $result = $loginHelper->authentication($email, md5($password));

            if ($_COOKIE['backUrl']){
              $this->_redirect($_COOKIE['backUrl'], array());
            }else{
              setcookie( 'backUrl');
              $this->_redirect("./", array());
            }
        }
//            
//            
//            
//            $result = $loginHelper->authenticate($email, md5($password));
//            if ($result->isValid()){
//
//                $loginHelper->authorization($result->getIdentity());
//                $auth = Zend_Auth::getInstance();
//
//                if ($ajax){
//                    $phonesManager = new \Ria\Application\Manager\Phones();
//                    $phones = $phonesManager->getPhonesByUser($result->getIdentity()->getClientId())->getResult();
//                    $phoneStr = '';
//                    foreach ($phones as $phone) {
//                        $phoneStr.=$phone->getPhoneStr() . ';   ';
//                    }
//                    $user_phones = $phoneStr;
//                    
//                    $user = $result->getIdentity();
//                    if (method_exists($user, 'getClientsNick') && $user->getClientsNick()) {
//                        $nick = $user->getClientsNick()->getPhoto();
//                        if ($nick) {
//                            $user_photo = $this->view->getImage($nick, 's');
//                        } else {
//                            $user_photo = 'http://auto.ria.ua/img/common/test/photo-smoll.png';
//                        }
//                    } else {
//                        $user_photo = 'http://auto.ria.ua/img/common/test/photo-smoll.png';
//                    }
//                    $this->view->status = 'ok';
//                    $name = array();
//                    $name['name'] = $result->getIdentity()->getFirstName() . ' ' .$result->getIdentity()->getLastName();
//                    $name['url'] = $profile_url;
//                    $name['title'] = _('Change profile');
//                    $name['exit_url'] = $exit_url;
//                    $name['exit'] = _('Exit');
//                    $name['phones'] = $user_phones;
//                    $name['photo'] = $user_photo;
//                    $this->view->name=$name;
//                }
//
//
//            }else{
//                $errors = $result->getMessages();
//                foreach ($errors as $key => $value) {
//                    if ($value=='password invalid'){
//                        unset($errors[$key]);
//                        $errors['email']= $this->view->translate('The above E-mail is in the database, but you have specified the correct password. Please select from the following: •  re-enter your password • continue entering data and get a new password via e-mail');
//                    }
//                }
//                $this->view->errors = $errors;
//            }
//        } else{ 
//             $errors = $loginForm->getMessages();
//             $this->view->errors = $errors;
//        }


//        print_r($result);
//        die('loginAction');
        
    }
    
    
     public function logoutAction() {
        $auth = Zend_Auth::getInstance();
        $auth->clearIdentity();
        $this->_redirect('./', array());
    }
}
?>
